<!-- 车辆补贴新增编辑 -->
<template>
	<view class="page">
		<u--form labelPosition="top" :model="model" ref="form" :rules="rules" labelWidth="auto" :labelStyle="{
				fontSize: '14px',
				color: '#B2B2B2',
				lineHight: '20px'
			}">
			<view class="basic-style">
				<view class="basic-title basic-border">新增</view>
				<u-form-item label="单据编号" prop="billNo" :borderBottom="false" required>
					<u--input disabled placeholder="请输入单据编号" v-model="model.billNo"></u--input>
				</u-form-item>
				
				<u-form-item label="申请人" prop="handlerId" :borderBottom="false" required>
					<h-select-person-input style="width: 100%" disabled v-model="model.proposer" placeholder="请选择申请人"
						border="surround" suffixIcon="arrow-down-fill" :suffixIconStyle="{color: '#B2B2B2'}"
						objectField="model.handlerId"></h-select-person-input>
				</u-form-item>
				<u-form-item label="申请部门" prop="handlerDeptId" :borderBottom="false" required>
					<h-select-depart-input disabled v-model="model.proposerDeptId" :departName="model.proposerDeptText"
						placeholder="请选择部门" border="surround" style="width: 100%" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
						:title.sync="model.proposerDeptText"></h-select-depart-input>
				</u-form-item>
				<u-form-item label="申请时间" prop="createTime" :borderBottom="false" required>
					<u--input v-model="model.createTime" disabled placeholder="请选择申请时间" border="surround"
						suffixIcon="calendar-fill" :suffixIconStyle="{color: '#B2B2B2'}"></u--input>
				</u-form-item>
				<u-form-item label="所属团队" prop="belongTeam" required>
					<h-select-team-input style="width: 100%" v-model="model.belongTeam" objectField="model.belongTeam"
						placeholder="请选择所属团队" border="surround" :tdlx="3" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"></h-select-team-input>
				</u-form-item>
				<u-form-item label="薪级" prop="payGrades" :borderBottom="false" ref="item1">
					<h-dict-select style="width: 100%" v-model="model.payGrades" placeholder="请选择薪级"
						dictName="erp_pay_grade,salary_grade,id,del_flag = 0 and audit_status=2"
						suffixIcon="arrow-down-fill" :suffixIconStyle="{color: '#B2B2B2'}" disabled></h-dict-select>
				</u-form-item>
				<u-form-item label="车牌号" prop="carNumber" required>
					<u--input v-model="model.carNumber" placeholder="请输入车牌号"></u--input>
				</u-form-item>
				<u-form-item label="职务" prop="job" :borderBottom="false" ref="item1">
					<h-dict-select style="width: 100%" v-model="model.job" placeholder="请选择职务"
						dictName="erp_org_position_level,level_name,id,del_flag='0' and audit_status='2'"
						suffixIcon="arrow-down-fill" :suffixIconStyle="{color: '#B2B2B2'}" disabled></h-dict-select>
				</u-form-item>
				<u-form-item label="对应薪级" prop="parallelismPayGrades" :borderBottom="false" ref="item1" required>
					<h-dict-select style="width: 100%" v-model="model.parallelismPayGrades" placeholder="请选择对应薪级薪级"
						dictName="salary_level" suffixIcon="arrow-down-fill" :suffixIconStyle="{color: '#B2B2B2'}"
						@change="parallelismPayGradesChange"></h-dict-select>
				</u-form-item>
				<u-form-item label="车贴(元)" prop="carSubsidy" :borderBottom="false" ref="item1" required>
					<h-money-input placeholder="请输入车贴" v-model="model.carSubsidy"></h-money-input>
				</u-form-item>
				<u-form-item label="备注" prop="remark" :borderBottom="false" ref="item1">
					<u--textarea :confirm-type="null" count v-model="model.remark" confirmType="done" placeholder="请输入备注"
						:maxlength="500"></u--textarea>
				</u-form-item>
			</view>
			<view class="basic-style">
				<view class="basic-title basic-border">附件信息</view>
				<view class="py-1 space-y-1" style="width: 100%">
					<view style="color: red">提示：请等待附件上传完成，再点击保存，否则附件无法保存成功！</view>
					<h-upload-file v-model="model.attachments" :isIcon="false" />
					<file-list v-if="model.attachments" v-model="model.attachments" :isDelete="true"></file-list>
				</view>
			</view>
			<view class="space-x-1 px-1 btn_position">
				<u-button :disabled="isSubmit" type="primary" :plain="true" text="保存" @click="save()" />
				<u-button :disabled="isSubmit" type="primary" text="保存提交" @click="saveAndCommit()" style="margin-left: 40rpx;" />
			</view>
			<view style="height: 100rpx;"></view>
		</u--form>
		<!-- 流程弹窗 -->
		<ProcessNodeConfiguration ref="processConfig" @ok="onOk"></ProcessNodeConfiguration>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import fileList from '@/pages/synergy/components/fileList.vue'
	import { mapState } from 'vuex'
	import moment from 'moment'
	import {
		generatedFormNumber, // 获取单据编号
		addErpCarSubsidy, //添加车辆补贴申请-保存
	} from '@/api/index.js'
	import {edit,add,detail} from '@/api/hr/erpCarSubsidy.js'
	import ProcessNodeConfiguration from '@/components/ProcessNodeConfiguration/ProcessNodeConfiguration.vue' // 流程节点配置人员选择弹窗
	export default {
		components: {
			fileList,
			ProcessNodeConfiguration
		},
		data() {
			return {
				id: '',
				model: {
					billNo: '', // 单据编号
					proposer: '', // 申请人id
					proposerDeptId: '', // 申请部门id
					proposerDeptText: '', //申请部门
					createTime: '', // 申请时间
					payGrades: '', //薪级
					belongTeam: '', //所属团队
					carNumber: '', //车牌号
					parallelismPayGrades: '', //对应薪级
					job: '', //职务id
					carSubsidy: '', //车贴
					remark: '', //备注
					attachments: null, //附件信息
				},
				rules: {
					billNo:[
						{
							required: true,
							message: '请输入单据编号!',
							trigger: ['blur','change']
						}
					],
					createTime:[
						{
							required: true,
							message: '请选择申请时间!',
							trigger: ['blur','change']
						}
					],
					proposer:[
						{
							required: true,
							message: '请选择申请人!',
							trigger: ['blur','change']
						}
					],
					proposerDeptId:[
						{
							required: true,
							message: '请选择申请部门!',
							trigger: ['blur','change']
						}
					],
					belongTeam: [{
						required: true,
						message: '请选择所属团队!',
						trigger: ['blur','change']
					}],
					carNumber: [{
						required: true,
						message: '请输入车牌号!',
						trigger: ['blur','change']
					}],
					parallelismPayGrades: [{
						required: true,
						message: '请选择对应薪级!',
						trigger: ['blur','change']
					}],
					carSubsidy: [{
						type: 'number',
						required: true,
						message: '请输入车贴!',
						trigger: ['blur','change']
					}],
				},
				loading: false,
				//代码生成后需手动设置流程编码
				flowCode: 'onl_erp_car_subsidy',
				formUrl: "erp/workbench/carSubsidy/modules/ErpCarSubsidyForm",
				formUrlMobile: "/pages/synergy/ErpCarSubsidyList",
				beanName: 'erpCarSubsidyServiceImpl',
				processId: '',
				
				isSubmit: false, // 控制按钮是否可以点击
			}
		},
		onLoad(options) {
			if (options.id) {
				// 编辑
				this.edit(options.id)
				this.id = options.id
			} else {
				// 添加
				this.add()
			}
		},
		computed: {
		  ...mapState(['userInfo']),
		},

		methods: {
			// 选择对应薪级触发
			parallelismPayGradesChange(e) {
				this.model.carSubsidy = e
			},
			// 添加
			add() {
				this.getGeneratedFormNumber()
				this.model.createTime = moment().format('YYYY-MM-DD HH:mm:ss'); // 申请时间
				this.model.proposer = uni.getStorageSync('userInfo').empId; //申请人id
				this.model.proposerDeptId = uni.getStorageSync('userInfo').deptId; //申请部门id
				this.model.payGrades = uni.getStorageSync('userInfo').employee.payGradeId //薪级
				this.model.job = uni.getStorageSync('userInfo').employee.positionLevelId //职务
			},
			// 获取单据编号
			getGeneratedFormNumber() {
				generatedFormNumber({
					prefix: 'CLBT',
					subSystemCode: '04'
				}).then(res => {
					if (res.success) {
						this.$set(this.model, 'billNo', res.result)
						 this.$set(this.model,'bpmBizTitle',this.userInfo?.employee?.name + '-' + this.model.billNo);//标题
					}
				})
			},
			// 保存提交
			async saveAndCommit() {
				// await this.$refs.form.validate()
				const val = await this.$refs.form.validate()
				if (!val) return
				this.isSubmit = true
				try {
					this.loading = true
					var url = ''
					if (this.id) {
						url = edit
					} else {
						url = add
					}
				  const response = await url({
				    ...this.model,
				  })
				  this.$u.toast(response.message)
				  if (response.success) {
						var that = this
						// 获取流程列表
						if (this.model.id) {
							this.getProcessList(this.model.id)
						} else {
						// 判断添加的时候是否有返回id值
							if (response.result) {
								this.getProcessList(response.result)
							}
						}
						this.$refs.uToast.show({
							message: response.message,
							type: 'default',
						})
				  } else {
						this.$refs.uToast.show({
							message: response.message,
							type: 'error'
						})
						this.loading = false
						this.isSubmit = false
					}
				} finally {
				  // this.loading = false
				}
			},
			// 保存
			async save() {
				// const val = await this.$refs.form.validate()
				// if (!val) return
				let url = ''
				if (this.id) {
					url = edit
				} else {
					url = add
				}
				this.isSubmit = true
				this.loading = true
				url(this.model).then(res => {
					if (res.success) {
						this.$refs.uToast.show({
							message: res.message,
							type: 'default',
							complete () {
								this.isSubmit = false
								if (getCurrentPages().length > 1) {
									uni.navigateBack()
								} else {
									uni.reLaunch({
										url: '/pages/home/home'
									})
								}
								
							}
						})
					} else {
						this.$refs.uToast.show({
							message: res.message,
			  			type: 'error'
						})
						this.loading = false
						this.isSubmit = false
					}
				}).finally(() => this.loading = false)
			},
			
			// 获取流程列表
			getProcessList (id) {
				if (this.isSubmit) {
					this.$refs.processConfig.showPopup = true
					this.$refs.processConfig.formUrl = this.formUrl
					this.$refs.processConfig.formUrlMobile = this.formUrlMobile
					this.$refs.processConfig.flowCode = this.flowCode
					this.$refs.processConfig.edit(id, this.processId, this.beanName)
				}
			},
			// 提交流程成功
			onOk () {
				this.$refs.uToast.show({
					message: '提交成功',
					type: 'success',
					complete () {
						if (getCurrentPages().length > 1) {
							uni.navigateBack()
						} else {
							uni.reLaunch({
								url: '/pages/home/home'
							})
						}
						
					}
				})
			},
			// 编辑数据回显
			edit(id){
				detail({id}).then(res=>{
					this.model = res.result
				})
			}
		}
	}
</script>


<style lang="scss" scoped>
	@import url("@/assets/inputStyle.scss");
</style>